/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
package
{

import flash.display.InteractiveObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.utils.ByteArray;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="800", height="600", frameRate="21", backgroundColor="#E0EEEE")]

public class SWFContextTest extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function SWFContextTest() {
		super();
		super.addEventListener(Event.ADDED_TO_STAGE, this.creationComplete);
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function creationComplete(event:Event!):void {
		super.removeEventListener(Event.ADDED_TO_STAGE, this.creationComplete);
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var loader:Loader = new Loader();
		//super.addChild(loader);
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handler_complete);
		loader.load( new URLRequest('Loadee.swf'), new LoaderContext(false, ApplicationDomain.currentDomain) );
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

	private function handler_complete(event:Event):void {
		const li:LoaderInfo = event.target as LoaderInfo;
		//super.addChild(li.loader);

		const mc:MovieClip = li.content as MovieClip;
		mc.addFrameScript(
			0,
			function ():void {
				trace('frameConfig', this);
				var textField:TextField;
				mc.create1 = function (className:String):Object {
					trace('create1', this);
					const c:Class = this.loaderInfo.applicationDomain.getDefinition(className) as Class;
					return new c();
				};
				textField = mc.create1('flash.text.TextField');
				trace(textField);
			}
		);
		
		mc.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame);


		const textField:TextField = new TextField();
		this.formatTextField(textField);
		textField.x = 400;
		super.addChild(textField);
		
		//const c:Class = li.applicationDomain.getDefinition('flash.text.TextField') as Class;
		//const c:Class = (li.content as Object).create('flash.text.TextField') as Class;
		//const textField:TextField = (li.content as Object).create('flash.text.TextField') as TextField;
		//const textField:TextField = mc.create1('flash.text.TextField') as TextField;

		//const textField:TextField = new c() as TextField;
		
	}

	private function formatTextField(textField:TextField):void {
		const textFormat:TextFormat = textField.defaultTextFormat;
		textFormat.font = 'iFlash502';
		textFormat.size = 8;
		textField.embedFonts = true;
		textField.defaultTextFormat = textFormat;
		textField.autoSize = TextFieldAutoSize.LEFT;
		textField.text = '"Русские буквы" Hello, world!';
	}

	private var _count:uint = 3;

	private function handler_enterFrame(event:Event):void {
		if (this._count--)
			return;

		const mc:MovieClip = event.target as MovieClip;

		mc.removeEventListener(event.type, arguments.callee);
		trace(mc.create1);
		const textField:TextField = mc.create1.call(mc, 'flash.text.TextField') as TextField;
		//const textField:TextField = mc.create('flash.text.TextField') as TextField;
		this.formatTextField(textField);
		textField.x = 200;
		super.addChild(textField);
	}
}
}